@using Microsoft.Extensions.Options
@using OrchardCore.Admin
@using OrchardCore.Commerce.AddressDataType
@using OrchardCore.Commerce.Settings
@using OrchardCore.ContentManagement.Metadata.Models
@using OrchardCore.DisplayManagement
@using OrchardCore.Mvc.Utilities
@model AddressFieldViewModel

@inject IOptions<AdminOptions> AdminOptions

@{
    var hint = Model.PartFieldDefinition.GetSettings<AddressPartFieldSettings>().Hint;
    var addressElementName = "address_" + Model.PartFieldDefinition.Name.HtmlClassify();

    var adminPrefix = AdminOptions?.Value.AdminUrlPrefix ?? "Admin";
    var isAdmin = Context.Request.Path.Value?.Split('/').Contains(adminPrefix, StringComparer.OrdinalIgnoreCase) == true;
}

<div class="@addressElementName">
    <h3>@Model.PartFieldDefinition.DisplayName()</h3>

    @if (isAdmin && Model.Address.NameParts.Any())
    {
        <div class="row">
            <div class="mb-3 col-md-12">
                <label for="@Html.IdFor(model => model.Address.Name)">@T["Full Name"]</label>
            </div>
            <div class="mb-3 col-md-6">
                <input type="text"
                       id="@Html.IdFor(model => model.Address.Name)"
                       class="form-control content-preview-text address__name"
                       value="@Model.Address.Name"
                       disabled>
                <span class="hint">
                    @T["The address contains name parts that were combined into this full name (<code>{0}</code>).", $"{nameof(Address)}.{nameof(Address.Name)}"]
                    @T["Most features use this unified name string instead of the name parts, as those can be region dependent."]
                </span>
            </div>
        </div>
    }

    @await DisplayAsync(
        new AddressFieldEditorViewModel
        {
            AddressField = Model.AddressPart,
            CityName = Html.NameFor(model => model.Address.City),
            Regions = Model.Regions,
            Provinces = Model.Provinces,
            Metadata = { Differentiator = $"{AddressFieldEditorViewModel.ShapeType}-{(Model as IShape)?.Metadata.Differentiator}" },
        })

    @if (!string.IsNullOrWhiteSpace(hint))
    {
        <span class="hint">@hint</span>
    }
</div>
